package org.lee.springcloud.provider.controllers;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author jackielee
 * @version 1.0
 * @description HelloController
 * @date 2021/3/10
 */
@RestController
public class HelloController {

  private final Logger logger = LoggerFactory.getLogger(HelloController.class);

  @Value("${server.name}")
  private String serverName;

  @GetMapping("/hello")
  public String index(@RequestParam String name) {
    return "hello " + name + ", this is first message(from " + serverName + ").";
  }

  /**
   * 加入超时
   *
   * @param name
   * @return
   */
  @GetMapping("/hello2")
  public String index2(@RequestParam String name) {
    logger.info("request second name is " + name);
    try {
      Thread.sleep(1000000);
    } catch (Exception e) {
      logger.error("hello second error", e);
    }
    return "hello " + name + ", this is second message(from " + serverName + ").";
  }

  /**
   * 测试gateway:AddRequestParameter过滤器
   *
   * @param foo
   * @return
   */
  @GetMapping("/foo")
  public String foo(String foo) {
    return "hello " + foo + "!(from " + serverName + ").";
  }

  @RequestMapping("/why")
  public String why(String name) {
    return "why are you " + name + "?(from " + serverName + ")";
  }
}
